package be.myandroid.games;

import javax.microedition.khronos.opengles.GL;

import be.myandroid.games.spritetext.MatrixTrackingGL;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.Window;

public class BlastPointsActivity extends Activity {
    private static final String PREFS_NAME = "SettingsStorage";
    
	private HelloOpenGLES10SurfaceView mGLView;
    
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		StateManager.getInstance().gameStop();
		Log.d("Activity", "GameStop");
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        new GameLevel(settings.getInt("currentLevel", 0)) {
			
			@Override
			public void storeProgress(int currentLevel) {
		        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		        Editor editor = settings.edit();
		        editor.putInt("currentLevel", currentLevel);
		        editor.commit();
			}
		};
        
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // Create a GLSurfaceView instance and set it
        // as the ContentView for this Activity.
        mGLView = new HelloOpenGLES10SurfaceView(this);
        setContentView(mGLView);
        Log.d("Activity","OnCreate");
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        // The following call pauses the rendering thread.
        // If your OpenGL application is memory intensive,
        // you should consider de-allocating objects that
        // consume significant memory here.
        mGLView.onPause();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // The following call resumes a paused rendering thread.
        // If you de-allocated graphic objects for onPause()
        // this is a good place to re-allocate them.
        mGLView.onResume();
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		StateManager.getInstance().TogglePauze();
		return false;
	}
}

class HelloOpenGLES10SurfaceView extends GLSurfaceView {
    public HelloOpenGLES10Renderer mRenderer;
    
    public HelloOpenGLES10SurfaceView(Context context){
        super(context);
        setGLWrapper(new GLSurfaceView.GLWrapper() {
            public GL wrap(GL gl) {
                return new MatrixTrackingGL(gl);
            }});
        // set the mRenderer member
        mRenderer = new HelloOpenGLES10Renderer(context);
        setRenderer(mRenderer);
        
        // Render the view only when there is a change
        //setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }
    
    
    @Override 
    public boolean onTouchEvent(MotionEvent e) {
        // MotionEvent reports input details from the touch screen
        // and other input controls. In this case, you are only
        // interested in events where the touch position changed.

        float x = e.getX();
        float y = e.getY();
    	float xPos = (2*x / getWidth() - 1f);
    	float yPos = -(2*y / getHeight() - 1f);
        
    	TouchManager.getInstance().OnTouchEvent(xPos, yPos, e.getAction());
    	
        return true;
    }
    
}